package org.infodavid.common.impl.services;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.infodavid.common.util.PropertiesUtil;
import org.joda.time.DateTime;

/**
 * The Class AbstractDailyProcessingBean.
 */
public abstract class AbstractDailyProcessingBean extends AbstractProcessingBean {

  /** The Constant DEFAULT_HOUR. */
  public static final byte DEFAULT_HOUR = 23;

  /** The Constant DEFAULT_MINUTE. */
  public static final byte DEFAULT_MINUTE = 0;

  /** The Constant log. */
  private static final Log LOGGER = LogFactory.getLog(AbstractDailyProcessingBean.class);

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.impl.services.AbstractProcessingBean#getProcessingDate()
   */
  @Override
  protected Date getProcessingDate() {
    final byte[] dailyTime = getDailyTime();
    final DateTime now = new DateTime();
    DateTime result;

    result = now.withHourOfDay(dailyTime[0]).withMinuteOfHour(dailyTime[1]);

    if (result.isBeforeNow()) {
      // tomorrow
      result = result.plusDays(1);
    }

    return result.toDate();
  }

  /**
   * Gets the daily time.
   * @return the daily time (hour and minute)
   */
  private byte[] getDailyTime() {
    final boolean isDebugEnabled = LOGGER.isDebugEnabled();
    final String name = getName();
    String value = PropertiesUtil.getInstance().getSystemPropertyValue(name + ".hour", null);
    final byte[] results = new byte[2];

    try {
      results[0] = Byte.parseByte(value);

      if (results[0] < 0 || results[0] > 23) {
        LOGGER.info("Invalid hour in the daily time definition, check system property: " + value
            + ", using default one: " + DEFAULT_HOUR);

        results[0] = DEFAULT_HOUR;
      }
    }
    catch (final Exception e) {
      if (isDebugEnabled) {
        LOGGER.debug(
            "Invalid hour in the daily time definition, check system property, using default one: "
                + DEFAULT_HOUR, e);
      }

      LOGGER
          .info("Invalid hour in the daily time definition, check system property, using default one: "
              + DEFAULT_HOUR);

      results[0] = DEFAULT_HOUR;
    }

    value = PropertiesUtil.getInstance().getSystemPropertyValue(name + ".minute", null);

    try {
      results[1] = Byte.parseByte(value);

      if (results[0] < 0 || results[0] > 59) {
        LOGGER.info("Invalid minute in the daily time definition, check system property: " + value
            + ", using default one: " + DEFAULT_MINUTE);

        results[1] = DEFAULT_MINUTE;
      }
    }
    catch (final Exception e) {
      if (isDebugEnabled) {
        LOGGER.debug(
            "Invalid minute in the daily time definition, check system property, using default one: "
                + DEFAULT_MINUTE, e);
      }

      LOGGER
          .info("Invalid minute in the daily time definition, check system property, using default one: "
              + DEFAULT_MINUTE);

      results[1] = DEFAULT_MINUTE;
    }

    if (isDebugEnabled) {
      LOGGER.debug("Daily time is " + results[0] + ":" + results[1]);
    }

    return results;
  }
}
